import pymysql


class DBTools(object):
    @staticmethod
    def __create_conn():
        conn = pymysql.connect(host="211.103.136.244", port=7061, user="student", password="iHRM_student_2022",
                               database="ihrm", charset="utf8")
        return conn

    @staticmethod
    def query_one(sql):
        cursor = None
        conn = None
        res = None
        try:
            conn = DBTools.__create_conn()  # 用类名调用静态方法
            cursor = conn.cursor()
            cursor.execute(sql)  # 执行用户传入的查询sql语句
            res = cursor.fetchone()

        except Exception as e:
            print("运行出现错误:", e)
        finally:
            cursor.close()
            conn.close()
            return res

    def query_all(self, sql):
        pass

    def query_many(self, sql):
        pass

    @staticmethod
    def db_iud(sql):
        """增删改"""
        cursor = None
        conn = None
        try:
            conn = DBTools.__create_conn()  # 用类名调用静态方法
            cursor = conn.cursor()
            cursor.execute(sql)  # 执行用户传入的查询sql语句
            conn.commit()  #
            print("增删改成功")

        except Exception as e:
            print("运行出现错误:", e)
            conn.rollback()
        finally:
            cursor.close()
            conn.close()


if __name__ == '__main__':
    DBTools.db_iud("delete from t_book where id=1234;")
